package io.bluerain.aclient.core;

import io.bluerain.converter.JsonUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

/**
 * Created by wscl on 2015/6/14.
 */
public class Response {

    private javax.ws.rs.core.Response response;
    private String body;

    public Response(javax.ws.rs.core.Response response) {
        this.response = response;
        this.body = this.response.readEntity(String.class);
    }

    public String readBody() {
        return this.body;
    }

    @Override
    public String toString() {
        return readBody();
    }

    public Document readDom() {
        return Jsoup.parse(this.body);
    }

    public <T> T readEntity(Class<T> cls) {
        return JsonUtil.readJson(body, cls);
    }

    public void result(Result result) {
        if (response.getStatus() == 200)
            result.success(this.body, this);
        else
            result.error(this.body, this.response);
    }
}
